<?php

class AddressMode extends SiteMode {
    /** @return AddressMode */
    public static function init() {
        return parent::init();
    }

    // 添加收货地址
    function add($user_id,$receiver,$phone,$address_id = null,$address_text = null,$default = 0){
        $data['user_id']=$user_id;
        $data['receiver']=$receiver;
        $data['phone']=$phone;
        $data['address_id']=$address_id;
        $data['address_text']=$address_text;
        $data['default']=$default;
        return $this->db->insert('user_address',$data,true) ==1 ? (int)$this->db->insertId():-1;
    }
    function delete($id){
        return $this->db->delete('user_address',$id) == 1;
    }
    function getDetail($id){
        $sql="SELECT * FROM `user_address` WHERE `id`=?";
        return $this->db->getOne($sql,$id);
    }

    /**
     * 搜用户所有地址
     * @param $user_id
     * @return array
     */
    function getList($user_id){
        //这里在拿个镇级的id
        //$sql="SELECT * FROM `user_address` WHERE `user_id`=? ORDER BY `default` DESC";

        //$sql="SELECT `user_address`.*,`base_address`.pid FROM `user_address`,`base_address` WHERE `user_id`=? and `user_address`.address_id=`base_address`.id ORDER BY `default` DESC";
        $sql="SELECT U.*,B.pid FROM `user_address` U LEFT JOIN `base_address` B on U.address_id=B.id
            where U.`user_id`=? and B.site_id=?  ORDER BY `default` DESC";
        return $this->db->getAll($sql,(int)$user_id,$this->siteId);
    }
    function getDefault($user_id){
        $sql="SELECT * FROM `user_address` WHERE `user_id`=? AND `default`=1";
        return $this->db->getOne($sql,$user_id);
    }
    /**
     * 设置默认地址（！已经默认了就不能再设默认了）
     * @param int $user_id
     * @param int $id 收货地址id
     * @return bool|int
     */
    function setDefault($user_id,$id){
        $data['default']=1;

        $this->db->beginTransaction();

        $up = $this->db->update('user_address',$id,$data);
        if($up == 1){
            $countSql="SELECT count(*) FROM `user_address` WHERE `user_id`=? AND `id`<>?";
            $count=(int)$this->db->getValue($countSql,$user_id,$id);
            $sql="UPDATE `user_address` SET `default`=0 WHERE `user_id`=? AND `id`<>?";
            $r=(int)$this->db->sqlExec($sql,$user_id,$id);
            if($count == $r){
                $this->db->commit();
                return true;
            }
        }
        $this->db->rollBack();
        return false;
    }
    //用户收货地址是否为空,为空新建地址要设为*默认哦~
    function checkEmpty($user_id){
        $sql="SELECT * FROM `user_address` WHERE `user_id`=?";
        return !$this->db->getExist($sql,$user_id);
    }

    // 获得一个地址default字段的值
    function getAddressDefault($id){
        $sql="SELECT `default` FROM `user_address` WHERE `id`=?";
        return $this->db->getValue($sql,$id);
    }

    //把用户收货地址用字符串输出来
    function getUserAddressAsString($id){
        $data=$this->getDetail($id);
        if(empty($data['address_id'])){
            return $data['receiver'].' ('.$data['phone'].') '.$data['address_text'];
        }
        $addressStr=$this->getDetailAsString($data['address_id']);
        return $data['receiver'].' ('.$data['phone'].') '.$addressStr.' '.$data['address_text'];
    }
    function getDetailAsString($address_id){
        $areaMode=AreaModule::init();
        $areaData=$areaMode->getFullAddress($address_id);
        $addressOneArr=array_map(function($v){
            return $v['name'];
        },$areaData);
        return implode('',$addressOneArr);
    }
} 